package com.heima.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.user.dtos.UserRelationDto;
import com.heima.model.user.pojos.ApUser;
import com.heima.model.user.pojos.ApUserFan;
import com.heima.model.user.pojos.ApUserFollow;
import com.heima.user.mapper.ApUserFollowMapper;
import com.heima.user.mapper.ApUserMapper;
import com.heima.user.service.ApUserFanService;
import com.heima.user.service.ApUserFollowService;
import com.heima.utils.thread.AppThreadLocalUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

@Transactional
@Service
@Slf4j
public class ApUserFollowServiceImpl extends ServiceImpl<ApUserFollowMapper, ApUserFollow> implements
        ApUserFollowService {
    @Autowired
    private ApUserMapper apUserMapper;
    @Autowired
    private ApUserFanService apUserFanService;
    @Override
    public ResponseResult follow(UserRelationDto dto) {
        ApUser  authorUser = apUserMapper.selectById(dto.getAuthorId());
        Integer id = AppThreadLocalUtil.getUser().getId();
        ApUser apUser = apUserMapper.selectById(id);
        Short operation = dto.getOperation();
        ApUserFan apUserFan = new ApUserFan();
        ApUserFollow apUserFollow = new ApUserFollow();
        if(operation==0){
            log.info("关注---------");
            apUserFollow.setUserId(AppThreadLocalUtil.getUser().getId());
            apUserFollow.setCreatedTime(new Date());
            apUserFollow.setFollowId(dto.getAuthorId());
            apUserFollow.setFollowName(authorUser.getName());
            apUserFollow.setLevel(0);
              this.save(apUserFollow);
            apUserFan.setUserId(dto.getAuthorId());
            apUserFan.setFansId(apUser.getId());
            apUserFan.setFansName(apUser.getName());
            apUserFan.setCreatedTime(new Date());
            apUserFan.setLevel((byte) 0);
            apUserFanService.save(apUserFan);
            log.info("关注成功");
    }else {
            LambdaQueryWrapper<ApUserFollow> queryWrapper1 = new LambdaQueryWrapper<ApUserFollow>()
                    .eq(apUser.getId() != null, ApUserFollow::getUserId, apUser.getId());
            LambdaQueryWrapper<ApUserFan> queryWrapper2 = new LambdaQueryWrapper<ApUserFan>()
                    .eq(apUser.getId() != null, ApUserFan::getFansId, apUser.getId());
            log.info("取关-----");
            this.remove(queryWrapper1);
            apUserFanService.remove(queryWrapper2);
            log.info("取关成功");
        }
        return  ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
